package com.wangshidai.util;


import cn.hutool.core.util.StrUtil;
import com.wangshidai.entity.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;
@Slf4j
@RestControllerAdvice  //全局异常处理类
public class CustomControllerAdvice {
    @ExceptionHandler(BindException.class)
    public R bindException(BindException e) {
        StringBuilder msg = new StringBuilder();
        try {
            List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
            fieldErrors.forEach(fieldError -> msg.append(fieldError.getDefaultMessage()).append(";"));
            if (StrUtil.isNotEmpty(msg)) {
                log.info("异常提示信息：" + msg);
            }
        } catch (Exception exception) {
            e.getMessage();
        }
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getFieldErrors();
        fieldErrors.forEach((oe) ->
                message.append("参数: [").append(oe.getObjectName())
                        .append(".").append(oe.getField())
                        .append("] 的传入值: [").append(oe.getRejectedValue()).append("] 与预期的字段类型不匹配.")
        );
        return new R(-1000,msg.toString(),  message);
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R formValid(MethodArgumentNotValidException e) {
        StringBuilder msg = new StringBuilder();
        try {
            List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
            fieldErrors.forEach(fieldError -> msg.append(fieldError.getDefaultMessage()).append(";"));
            if (StrUtil.isNotEmpty(msg)) {
                log.info("异常提示信息：" + msg);
            }
        } catch (Exception exception) {
            exception.getMessage();
        }
        StringBuilder message = new StringBuilder();
        List<FieldError> fieldErrors = e.getFieldErrors();
        fieldErrors.forEach((oe) ->
                message.append("参数: [").append(oe.getObjectName())
                        .append(".").append(oe.getField())
                        .append("] 的传入值: [").append(oe.getRejectedValue()).append("] 与预期的字段类型不匹配.")
        );
        return new R(-1000,msg.toString(),  message);
    }
    //抛出异常详细信息
    @ExceptionHandler(BusinessException.class)
    public R serviceException(BusinessException e) {
        try {
            return new R(-1000, e.getMessage(), null);
        } catch (Exception exception) {
            exception.getMessage();
        }
        return new R(-1000, e.getMessage(), null);
    }
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public R httpMessageNotReadableException(HttpMessageNotReadableException e) {
        return new R(-1000, "参数无法解析成JSON", null);
    }
    //MethodArgumentNotValidException：服务器数据校验异常类
//    @ExceptionHandler(BindException.class)
//    public R xxx(BindException e){
//        return new R(-1000,"1111",null);
//    }打开注释前请先注释掉bindException()方法

    //处理空指针
    @ExceptionHandler(NullPointerException.class)
    public R yyy(NullPointerException e){
        return new R(-1000,"2222",null);
    }
    //
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public R kkk(ArrayIndexOutOfBoundsException e){
        return new R(-1000,"3333",null);
    }

//    @ExceptionHandler(BusinessException.class)
//    public R kkk(BusinessException e){
//        return new R(-1000,e.getMessage(),null);
//    }打开注释前请先注释掉serviceException()方法


    @ExceptionHandler(Exception.class)
    public R yyyy(Exception e){
        return new R(-1000,"未知异常",null);
    }

}
